﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iLangl.Customer.DNN.Components;
using iLangl.Model;
using iLangl.Customer.Managers;
using DotNetNuke.Entities.Users;
using DotNetNuke.Common;
using iLangl;
using iLangl.Customer;
using iLangl.Plugin;
using System.Web.Security;

namespace iLangl.Customer.DNN
{
	public partial class Registration : DnnModuleBase
	{

		#region User Info
		public bool IsChangePassword
		{
			get
			{
				if (ViewState["IsChangePassword"] == null)
					return false;

				return (bool)ViewState["IsChangePassword"];
			}
			set
			{
				ViewState["IsChangePassword"] = value;
			}
		}

		public void UpdateUserInfo()
		{
			bool isUserRegistered = RegistrationManager.Instance.IsUserRegistered(this.SiteId);
			txtUserName.Enabled = !isUserRegistered;
			divUserPassword.Visible = IsChangePassword || !isUserRegistered;
			divConfirmUserPassword.Visible = IsChangePassword || !isUserRegistered;
			btnConfirmChangePassword.Visible = IsChangePassword;
			btnCancelChangePassword.Visible = IsChangePassword;
			btnChangePassword.Visible = !IsChangePassword;
		}

		protected void btnChangePassword_Click(object sender, EventArgs e)
		{
			IsChangePassword = true;
			UpdateUserInfo();
		}

		protected void btnConfirmChangePassword_Click(object sender, EventArgs e)
		{

		}

		protected void btnCancelChangePassword_Click(object sender, EventArgs e)
		{
			IsChangePassword = false;
			UpdateUserInfo();
		}
		#endregion

		protected void Page_Load(object sender, EventArgs e)
		{
			btnCancel.NavigateUrl = Globals.NavigateURL();
			if (!IsPostBack)
			{
				UpdateUserInfo();

				CustomerCompany company = CompanyManager.Instance.GetCompanyBySiteId(this.SiteId);

				if (company != null)
				{
					SetCompany(company);
				}
				else
				{
					binddefaultInfo();
				}
				changeTabsVisible(false);
			}
		}


		protected void binddefaultInfo()
		{
			UserInfo currentuser = UserController.GetCurrentUserInfo();
			txtUserName.Text = currentuser.Email;
			txtContactFirstName.Text = currentuser.FirstName;
			txtContactLastName.Text = currentuser.LastName;
			txtContactEmail.Text = currentuser.Email;

			if (currentuser.Profile != null)
			{

				txtContactPhone.Text = currentuser.Profile.Cell;
			
				txtCountry.Text = currentuser.Profile.Country;
				txtState.Text = currentuser.Profile.Region;
				txtCity.Text = currentuser.Profile.City;
				txtZip.Text = currentuser.Profile.PostalCode;
				txtAddress.Text = currentuser.Profile.Street + " " + currentuser.Profile.Unit;
				txtPhone.Text = currentuser.Profile.Telephone;


				txtBillingCity.Text = currentuser.Profile.City;
				txtBillingCountry.Text = currentuser.Profile.Country;

				txtBillingEmail.Text = currentuser.Email;
				txtBillingFirstName.Text = currentuser.FirstName;
				txtBillingLastName.Text = currentuser.LastName;
				txtBillingPostalCode.Text = currentuser.Profile.PostalCode;
				txtBillingStreetAddress.Text = currentuser.Profile.Street + " " + currentuser.Profile.Unit;
				
			}

			txtCompanyName.Text = this.PortalSettings.PortalName;
			txtName.Text = this.PortalSettings.PortalName;

			txtPrefferedCurrency.Text = this.PortalSettings.Currency;
			txtBillingCurrency.Text = this.PortalSettings.Currency;

			


		}

		protected void btnFinish_Click(object sender, EventArgs e)
		{
			try
			{
				PluginRegistration pg = GetPluginRegistration();
				RegistrationManager.Instance.RegisterLicense(this.SiteId, pg);

				ShowSuccessMessage(GetLocalizedString("lblStatus"));
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage(ex);
			}
		}

		protected void btnNext_Click(object sender, EventArgs e)
		{
			HttpCookie cookie = Request.Cookies["dnnTabs-tabSettingsForm"];
			if (cookie == null)
				return;

			int tabId = int.Parse(cookie.Value);

			if (tabId == 0) //Register User
			{
				RegisterUser();
				UpdateUserInfo();
			}

			if (tabId == 1)
			{
				OpenBillingInfoTab();
			}

			if (tabId == 2)
			{
				OpenConfirmationTab();
			}
		}


		protected void RegisterUser()
		{
			if (String.Compare(txtUserPassword.Text, txtConfirmUserPassword.Text, true) != 0)
			{
				ShowErrorMessage(GetLocalizedString("PasswordNotMatchedErrorMessage"));
				return;
			}

			try
			{
				
				if (!RegistrationManager.Instance.IsUserRegistered(this.SiteId))
				{
					String status = string.Empty;
					status = RegistrationManager.Instance.Authenticate(this.SiteId, txtUserName.Text, txtUserPassword.Text);

					if (status != MembershipCreateStatus.Success.ToString())
					{
						status = String.Format("l{0}", status);
						ShowErrorMessage(GetLocalizedString(status));
						return;
					}
				}
				
				
				//Authentication passed successfully
				OpenCompanyInfoTab();
			}
			catch (Exception ex)
			{
				//Handle error
				ShowErrorMessage(ex);
			}
		}

		protected void OpenCompanyInfoTab()
		{
			HttpCookie myCookie = new HttpCookie("dnnTabs-tabSettingsForm");
			myCookie.HttpOnly = false;

			myCookie.Value = "1";
			Response.Cookies.Add(myCookie);
			dnnCompanyInfo.Visible = true;
			dnnCompanyInfoDiv.Visible = true;
		}

		protected void OpenBillingInfoTab()
		{
			HttpCookie myCookie = new HttpCookie("dnnTabs-tabSettingsForm");
			myCookie.HttpOnly = false;

			myCookie.Value = "2";
			Response.Cookies.Add(myCookie);
			dnnBillingInfo.Visible = true;
			dnnBillingInfoDiv.Visible = true;
		}

		protected void OpenConfirmationTab()
		{
			HttpCookie myCookie = new HttpCookie("dnnTabs-tabSettingsForm");
			myCookie.HttpOnly = false;

			myCookie.Value = "3";
			Response.Cookies.Add(myCookie);
			dnnResult.Visible = true;
			dnnResultDiv.Visible = true;

			btnNext.Visible = false;
			btnFinish.Visible = true;
			btnNext.Text = GetLocalizedString("lFinish");
		}



		private void changeTabsVisible(bool flag)
		{
			dnnCompanyInfo.Visible = flag;
			dnnBillingInfo.Visible = flag;
			dnnCompanyInfoDiv.Visible = flag;
			dnnBillingInfoDiv.Visible = flag;
			dnnResult.Visible = flag;
			dnnResultDiv.Visible = flag;
		}

		public void SetCompany(CustomerCompany company)
		{
			IPluginConfiguration PG = ConfigurationFactory.Instance.CreateConfigurationManager().GetPluginConfiguration(this.SiteId);

			txtUserName.Text = PG.AccountUserName;

			txtAddress.Text = company.Address;
			txtCity.Text = company.City;
			txtCountry.Text = company.Country;
			txtPrefferedCurrency.Text = company.Currency;
			txtCompanyName.Text = company.Name;
			txtPhone.Text = company.Phone;
			txtState.Text = company.State;
			txtZip.Text = company.Zip;


			if (company.BillingInfo != null)
			{
				txtName.Text = company.BillingInfo.Name;
				txtBillingCity.Text = company.BillingInfo.BillingCity;
				txtBillingCountry.Text = company.BillingInfo.BillingCountry;
				txtBillingCurrency.Text = company.BillingInfo.BillingCurrency;
				txtBillingEmail.Text = company.BillingInfo.BillingEmail;
				txtBillingFirstName.Text = company.BillingInfo.BillingFirstName;
				txtBillingLastName.Text = company.BillingInfo.BillingLastName;
				txtBillingPostalCode.Text = company.BillingInfo.BillingPostalCode;
				txtBillingStreetAddress.Text = company.BillingInfo.BillingStreetAddress;
				txtBillingStreetAddress2.Text = company.BillingInfo.BillingStreetAddress2;
			}

			if ((company.Contacts != null) && (company.Contacts.Length >= 1))
			{
				txtContactEmail.Text = company.Contacts[0].Email;
				txtContactFirstName.Text = company.Contacts[0].FirstName;
				txtContactLastName.Text = company.Contacts[0].LastName;
				txtContactPhone.Text = company.Contacts[0].Phone;
			}

		}

		public PluginRegistration GetPluginRegistration()
		{

			PluginRegistration pr = RegistrationManager.Instance.GetPluginRegistration(this.SiteId);


			CustomerCompany company = CompanyManager.Instance.GetCompanyBySiteId(this.SiteId);

			if (company == null)
			{
				company = new CustomerCompany();
				company.Id = Guid.NewGuid().ToString();
				company.CustomerIdentity = this.SiteId;
				company.SiteId = this.SiteId;

			}
			
			company.Address = txtAddress.Text;
			company.City = txtCity.Text;
			company.Country = txtCountry.Text;
			company.Currency = txtPrefferedCurrency.Text;
			company.Name = txtCompanyName.Text;
			company.Phone = txtPhone.Text;
			company.State = txtState.Text;
			company.Zip = txtZip.Text;
		
			

			pr.Company = company;

			if (pr.Company.BillingInfo == null)
			{
				pr.Company.BillingInfo = new BillingInfo();
				pr.Company.BillingInfo.Id = Guid.NewGuid().ToString();
				pr.Company.BillingInfo.CustomerIdentity = this.SiteId;
			}

			company.BillingInfo.Name = txtName.Text;
			pr.Company.BillingInfo.BillingCity = txtBillingCity.Text;
			pr.Company.BillingInfo.BillingCountry = txtBillingCountry.Text;
			pr.Company.BillingInfo.BillingCurrency = txtBillingCurrency.Text;
			pr.Company.BillingInfo.BillingEmail = txtBillingEmail.Text;
			pr.Company.BillingInfo.BillingFirstName = txtBillingFirstName.Text;
			pr.Company.BillingInfo.BillingLastName = txtBillingLastName.Text;
			pr.Company.BillingInfo.BillingPostalCode = txtBillingPostalCode.Text;
			pr.Company.BillingInfo.BillingStreetAddress = txtBillingStreetAddress.Text;
			pr.Company.BillingInfo.BillingStreetAddress2 = txtBillingStreetAddress2.Text;


			Contact contact = null;
			if ((pr.Company.Contacts == null) || (pr.Company.Contacts.Length <= 0))
			{
				contact = new Contact();
				contact.Id = Guid.NewGuid().ToString();
				contact.CustomerIdentity = this.PortalSettings.UserId.ToString();
				pr.Company.AddContact(contact);
			}
			else
			{
				contact = company.Contacts[0];
			}

			pr.PrimaryContactId = contact.Id;
			contact.Email = txtContactEmail.Text;
			contact.FirstName = txtContactFirstName.Text;
			contact.LastName = txtContactLastName.Text;
			contact.Phone = txtContactPhone.Text;
			

			return pr;
		}



	}
}